import QtQuick
MouseArea{
    id:mouseArea

    //鼠标滑动的操作模式
    enum FlickMode
    {
        HoldFlick,//鼠标左键点击并滑动
        HoverFlick//在获取焦点的情况下将鼠标锁定在视图中央。直接滑动鼠标控制
    }

    property int flickMode:InputMouseArea.HoldFlick

    hoverEnabled: flickMode === InputMouseArea.HoldFlick?false:true

    property point lastPos
    property point centerPoint:Qt.point(width / 2,height / 2)

    signal flick(event:var)

    function resetMouse(){
        let center = mapToGlobal(width / 2,height / 2)
        CursorControl.setCursorPosition(center)
    }

    onPressed:
        (e)=>{
            lastPos.x = e.x
            lastPos.y = e.y
    }

    onActiveFocusChanged: {
        if(activeFocus)
        {
            if(flickMode === InputMouseArea.HoverFlick)
            {
                resetMouse()
            }
        }
    }

    onPositionChanged:
        (e)=>{
            var delta
            if(flickMode === InputMouseArea.HoldFlick)
            {
                delta = Qt.point(e.x - lastPos.x,e.y - lastPos.y)
            }
            else if(flickMode === InputMouseArea.HoverFlick){
                if(activeFocus)
                {
                    delta = Qt.point(e.x - centerPoint.x,e.y - centerPoint.y)
                    resetMouse()
                }
            }

            if(delta)
            {
                var event = {
                    x:delta.x / width,
                    y:delta.y / height,
                    button:e.button
                }
                lastPos.x = e.x
                lastPos.y = e.y
                flick(event)
            }
    }

    onClicked: {
        forceActiveFocus()
    }
}
